package com.smartens.boit.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.smartens.boit.R;
import com.smartens.boit.activity.EnergyDetailActivity;
import com.smartens.boit.activity.FaultFollowActivity;
import com.smartens.boit.adapter.EnergyAdapter;
import com.smartens.boit.adapter.LatestAdapter;
import com.smartens.boit.bean.StatusDetailBean;
import com.smartens.boit.util.HttpHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 能耗管理
 * @Author songtt
 * @Date 2021/3/4
 */
public class ThirdFragment extends Fragment {
    private RecyclerView recyclerView;
    private EnergyAdapter recycleAdapter;
    private LinearLayoutManager mLinearLayoutManager;
    private List<StatusDetailBean> list = new ArrayList<>();
    private HttpHelper helper;
    private static final String TAG = "ThirdFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_third, container, false);
        // 请求
        helper = HttpHelper.getInstance();
        recyclerView = view.findViewById(R.id.rv_latest);
        GetDeviceStatusListResult();
        mLinearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recycleAdapter = new EnergyAdapter(list);
        //设置布局管理器GetDevicesList
        recyclerView.setLayoutManager(mLinearLayoutManager);
        //设置Adapter
        recyclerView.setAdapter(recycleAdapter);
        //初始化视图
        recycleAdapter.setOnItemClickListener(new EnergyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), EnergyDetailActivity.class);
                startActivity(intent);
                 Log.v("=====", "点击详情");
            }
        });
        return view;
    }

    public void GetDeviceStatusListResult() {
        helper.GetDeviceStatusListResult2(new HttpHelper.HttpCallBack<StatusDetailBean>() {

            @SuppressLint("LongLogTag")

            @Override
            public void onSuccess(List<StatusDetailBean> result) {
                if (result.size() > 0) {
                    Log.d(TAG, "onSuccess: GetDeviceStatusListResult" + result.get(0));
                    recycleAdapter.getData(result);
                    recyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            recycleAdapter.notifyDataSetChanged();
                        }
                    });

                } else {
                    Log.d(TAG, "onSuccess: GetDeviceStatusListResult" + 0);

                }


            }

            @SuppressLint("LongLogTag")
            @Override
            public void onFailure(final String message) {
                 Log.v(TAG, "GetDeviceStatusList" + message);
            }
        });
    }


}